<?php
namespace Home\Controller;

use Util\AccessController;

class AddressController extends AccessController {

    /*
     * 获取用户所有地址列表
     */
    public function GetMyAddress() {
        $where['user_id'] = $_POST['user_id'];
        $LS = Service('Address', 'u_address');
        $data = $LS->query($where);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 添加收货地址数据样例
     */
    public function GetAddAddressData() {
        $data['address_id'] = 'a' . uniqid() . rand(0, 9);
        $data['ld_id'] = '1,2,3';
        $data['region_id_shen'] = '广东省';
        $data['region_id_shi'] = '深圳市';
        $data['region_id_qu'] = '南山区';
        $data['user_id'] = '36d85062-1970-471c-903b-f1cbf5828b6f';
        $data['address_detail'] = '西丽街道第一工业区H栋';
        $data['address_name'] = '大表哥';
        $data['address_phone'] = '13631297676';
        $this->ajaxReturn($data, 'JSON');
    }

    /*
     * 添加收货地址
     * 修改地址
     */
    public function AddAddress() {
        $data = $_POST;
        if ($_POST['address_id']) {
            $where['address_id'] = $_POST['address_id'];
            $LS = Service('Address', 'u_address');
            $OK = $LS->update($where, $data);
        } else {
            $data['address_id'] = 'a' . uniqid() . rand(0, 9);
            $LS = Service('Address', 'u_address');
            $OK = $LS->insert($data);
        }
        $DATA = ResData($OK);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 删除地址
     */
    public function DelAddress() {
        $IDS = explode(',',$_POST['address_id']);
        $LS = Service('Address', 'u_address');
        $OKS = 1;
        foreach($IDS as $k=>$v){
            $where['address_id'] = $IDS[$k];
            $OK = $LS->remove($where);
            $OK?1:$OKS=0;
        }
        $DATA = ResData($OKS);
        $this->ajaxReturn($DATA, 'JSON');
    }

}